package com.jr.controller;

import com.alibaba.fastjson.JSON;
import com.jr.entity.Order;
import com.jr.service.OrderService;
import com.jr.service.impl.OrderServiceImpl;
import com.jr.util.PageHelper;
import com.jr.util.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("*.order")
@MultipartConfig
public class OrderController extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        // 2.获取到addVip
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 6);

        try {
            // 3.利用反射获取对应的方法对象
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 4.执行对应的方法
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageHelper<Order> pageHelper = new PageHelper<>();

        // 处理当前页
        String currentPage = request.getParameter("currentPage");
        currentPage = currentPage == null || ("".equals(currentPage)) ? "1" : currentPage.trim();
        // 设置当前页
        pageHelper.setCurrentPage(Integer.parseInt(currentPage));
        PrintWriter out = response.getWriter();
        try {
//            List<Student> students = studentService.listStudents();
            //            out.write(JSON.toJSONString(new ResultUtil("students",students)));
            // 带有分页功能的数据返回给前台

            pageHelper.setTotal(orderService.getOrderTotal((Integer) request.getSession().getAttribute("enterpriseId")));
            List<Order> orders = orderService.listOrder((Integer) request.getSession().getAttribute("enterpriseId"),pageHelper.getStart(), pageHelper.getPageSize());
            pageHelper.setData(orders);
            out.write(JSON.toJSONString(new ResultUtil("page",pageHelper)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            out.close();
        }
    }

    /**
     * 根据id查询相关订单的详细信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void invoiceDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            List<Order> invoiceOrder = orderService.invoiceOrder(Integer.valueOf(request.getParameter("id")));
            out.write(JSON.toJSONString(new ResultUtil("invoiceOrder",invoiceOrder)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 在详情页里显示当前开票信息的总金额
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void totalAmount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            Double totalAmount = orderService.TotalAmount(Integer.valueOf(request.getParameter("id")));
            out.write(JSON.toJSONString(new ResultUtil("totalAmount",totalAmount)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 订单的总金额
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void sumAmount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            Double sumAmount = orderService.sumAmount();
            out.write(JSON.toJSONString(new ResultUtil("sumAmount",sumAmount)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

    /**
     * 已经开票的订单的总金额
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void finishSumAmount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            Double finishSumAmount = orderService.finishSumAmount();
            out.write(JSON.toJSONString(new ResultUtil("finishSumAmount",finishSumAmount)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
